<?php 

function checkSettingsValidity($numPhases) {
	for ($j = 1; $j <= $numPhases; $j++) {
		if(!isset($_POST['phase_'.$j]) || 
			!isset($_POST['sdatey_'.$j]) || !isset($_POST['sdatem_'.$j]) || !isset($_POST['sdatey_'.$j]) || 
			!isset($_POST['edatey_'.$j]) || !isset($_POST['edatem_'.$j]) || !isset($_POST['edated_'.$j]) )
			{
				echo "SOMETHING IS NOT SET, j=".$j."<br/>";
				return false;
			}
		
	}
	return true;
}

function submitSettings($numPhases) {
	$ret = checkSettingsValidity();
	if($ret){
		for ($j = 1; $j <= $numPhases; $j++) {
			
		}
		
	}
}
	

	$numPhases = 0;
	ConnectToDB();
	
	$phasesRec = DBExecute("SELECT COUNT(phase) as num FROM Phases");
	if($rowNum = $phasesRec->fetchAssocRow())
		$numPhases = $rowNum['NUM'];
	
	$phasesRec = DBExecute("SELECT phase, 
								year(starttime) as syear, month(starttime) as smonth, day(starttime) as sday, 
								year(endtime) as eyear, month(endtime) as emonth, day(endtime) as eday
								FROM Phases");
		
	if(isset($_POST['checksetpost'])){
		submitSettings($numPhases);
		$_POST['checksetpost'] = null;
	}
?>

<div>
	<form id="confer-settings" action="" method="post">
	<table>
		<tr><th>Phase Name</th><th>Start Date</th><th>End Date</th></tr>
		<?php
		$i = 1;
		while( $phase = $phasesRec->fetchAssocRow()){ 
		?>
		<tr>
			<td><input type="text" name="<?php echo "phase_".$i?>" size="15" value="<?php echo $phase['PHASE']; ?>" readonly/></td>
			<td>
			year:<input type="text" name="<?php echo "sdatey_".$i?>" size="4" value="<?php echo $phase['SYEAR']; ?>" />
			month:<input type="text" name="<?php echo "sdatem_".$i?>" size="2" value="<?php echo $phase['SMONTH']; ?>" />
			day:<input type="text" name="<?php echo "sdated_".$i?>" size="2" value="<?php echo $phase['SDAY']; ?>" />
			</td>
			<td>
			year:<input type="text" name="<?php echo "edatey_".$i?>" size="4" value="<?php echo $phase['EYEAR']; ?>" />
			month:<input type="text" name="<?php echo "edatem_".$i?>" size="2" value="<?php echo $phase['EMONTH']; ?>" />
			day:<input type="text" name="<?php echo "edated_".$i?>" size="2" value="<?php echo $phase['EDAY']; ?>" />
			</td>
		</tr>
		<?php
		} 
		?>		
	</table>
		<input type="hidden" name="checksetpost" value=true />
		<input type="submit" class="button" value="Submit changes" />
	</form>
</div>